angular.module('app.baseinfo')
    .controller('userCtrl', ['$scope', '$filter','$location', '$modal', 'toaster', 'userService',
        function ($scope, $filter,$location, $modal, toaster, userService) {
            $scope.users = [];
            $scope.search;
            $scope.pager = {
                pageNo: 1,
                pageSize: 10,
                totalItems: 0
            };


            $scope.searchUser = function () {
                var query = {};
                query.search = $scope.search;
                query.pageNo = $scope.pager.pageNo;
                query.pageSize = $scope.pager.pageSize;

                userService.getUsers(query).then(function (res) {
                    $scope.users = res.data.data;
                    $scope.pager.totalItems = res.data.totalItems;
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "获取信息失败")
                });
            };
            $scope.searchUser();

            $scope.scoreRecord = function(user){
                var id = user.id;
                var url = '/baseinfo/userInfo/scoreRecord/' + id;
                //window.open(url);
                $location.path(url);
            };

            $scope.orderRecord = function(user){
                var id = user.id;
                var url = '/baseinfo/userInfo/orderRecord/' + id;
                //window.open(url);
                $location.path(url);
            }



            $scope.removeRecord = function(e){
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/confirm/confirm.html',
                    controller: 'confirmCtrl',
                    size: 'sm',
                    resolve: {
                        content: function () {
                            return "确认清空该用户的所有消费记录?";
                        }
                    }
                });
                modalInstance.result.then(function () {
                    userService.removeRecord(e).then(function (res) {
                        toaster.pop("success", "成功", "清空成功");
                        $scope.searchUser();
                    }, function (res) {
                        toaster.pop("error", "错误", res.errMsg || "清空失败")
                    });
                });
            };

        }])
    .controller('scoreRecordCtrl', ['$scope', '$filter', '$modal','$routeParams','$window', 'toaster', 'userService',
        function ($scope, $filter, $modal,$routeParams,$window, toaster, userService) {
            $scope.startTime;
            $scope.endTime;
            $scope.pager = {
                pageNo: 1,
                pageSize: 10,
                totalItems: 0
            };
            $scope.options = {
                allowInvalid: false,
                timezone: 'CCT'
            };
            $scope.search;
            $scope.scoreRecords=[];

            $scope.back = function(){
                $window.history.back();
            }
            $scope.search = function(){
                var query={};
                query.search = $scope.search;
                query.pageNo = $scope.pager.pageNo;
                query.pageSize = $scope.pager.pageSize;
                query.userId = $routeParams.userId;
                query.startTime =$filter('date')($scope.startTime,'yyyy-MM-dd HH:mm:ss');
                query.endTime = $filter('date')($scope.endTime,'yyyy-MM-dd HH:mm:ss');
                userService.scoreRecordsList(query).then(function (res) {
                    $scope.scoreRecords = res.data.data;
                    $scope.pager.totalItems = res.data.totalItems;
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "获取信息失败")
                });
            };

            $scope.search();

            $scope.addScore = function(){
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/baseinfo/add_score_record.html',
                    controller: 'recordModelCtrl',
                    size: 'md',
                    resolve: {
                       recordDetail: function () {
                            return {userId:$routeParams.userId};
                        }
                    }
                });
                modalInstance.result.then(function (recordDetail) {
                    $scope.search();
                });
            };
        }])
    .controller('recordModelCtrl', ['$scope', '$filter', '$modal', '$window', '$routeParams', '$modalInstance',
        'toaster', 'userService', 'recordDetail',
        function ($scope, $filter, $modal, $window, $routeParams, $modalInstance, toaster, userService, recordDetail) {

            $scope.title = "积分添加";
            $scope.scoreType = [{name:"消费加积分",type:1},{name:"消费积分",type:2},
                {name:"手动添加积分",type:3},{name:"手动消除积分",type:4}]

            $scope.recordDetail = recordDetail;

            $scope.cancel = function () {
                $modalInstance.dismiss('cancel');
            };

            $scope.selectType = function($select){
                $scope.recordDetail.typeName = $select.name;
                $scope.recordDetail.type = $select.type;
            };
            $scope.save = function () {
                $scope.recordDetail.recordTime = new Date();
                userService.addRecord($scope.recordDetail).then(function (res) {
                    $modalInstance.close($scope.material);
                    toaster.pop("success", "成功", "添加积分项成功")
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "获取信息失败")
                });
            };
        }])
    .controller('orderRecordCtrl', ['$scope', '$filter', '$modal','$routeParams','$window', 'toaster', 'userService','orderService',
        function ($scope, $filter, $modal,$routeParams,$window, toaster, userService,orderService) {
            $scope.startTime;
            $scope.endTime;
            $scope.pager = {
                pageNo: 1,
                pageSize: 10,
                totalItems: 0
            };
            $scope.options = {
                allowInvalid: false,
                timezone: 'CCT'
            };
            $scope.query;
            $scope.scoreRecords=[];

            $scope.back = function(){
                $window.history.back();
            }
            $scope.search = function(){
                var query={};
                query.pageNo = $scope.pager.pageNo;
                query.pageSize = $scope.pager.pageSize;
                query.userId = $routeParams.userId;
                query.startTime =$filter('date')($scope.startTime,'yyyy-MM-dd HH:mm:ss');
                query.endTime = $filter('date')($scope.endTime,'yyyy-MM-dd HH:mm:ss');
                query.status = 1;
                orderService.orderList(query).then(function (res) {
                    $scope.orderRecords = res.data.data;
                    $scope.pager.totalItems = res.data.totalItems;
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "获取信息失败")
                });
            };

            $scope.search();

        }])
